import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Window 2.1
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.1

import Enginio 1.0
import QtMultimedia 5.2

Window {
    id: mainWindow
    title: "Runing"
    flags: Qt.Dialog
    width: 400
    height: 640

    property string scheduleName
    property string currentExercise
    signal backPressed

    property int currentIndex: -1
    property int leftSeconds: 0

    Timer {
        id: timer
        repeat: false

        onTriggered: {
            console.log("onTriggered ;" + leftSeconds + " ; " + currentIndex + " ; " + intervalView.count)

            timer.start()

            if ( leftSeconds > 0 )
            {
                leftSeconds--
//                timer.start()
            }
            else
            {
                if ( currentIndex < intervalView.count - 1 )
                {
                    currentIndex++
                    exView.currentIndex = currentIndex
                    intervalView.currentIndex = currentIndex
                    currentExercise = exView.currentItem.text
                    leftSeconds = intervalView.currentItem.text*60

                    playMusic.play()
                }
                else if ( mainWindow.visible && currentIndex == intervalView.count - 1)
                {
                    playMusic.play()
                }
                else
                {
//                    timer.start()
                }
            }
        }
    }

    Audio {
        id: playMusic
        source: "qrc:/sounds/beep.mp3"

        onStopped: {
            timer.start()
        }
   }


    Rectangle {
        id: mainRect
        color: "#f4f4f4"
        anchors.fill: parent
        focus: true

        Header {
            id: header
            iconSource: "qrc:images/run.png"
            title: scheduleName
        }
        ListView {
            id: exView
            model: enginioModel
            delegate: Component {
                Text {
                    text: exercise
                }
            }
            anchors.top: header.bottom
            anchors.bottom: mainRect.bottom
            width: parent.width
            clip: true
            visible: false
        }
        ListView {
            id: intervalView
            model: enginioModel
            delegate: Component {
                Text {
                    text: interval
                }
            }
            anchors.top: exView.top
            anchors.bottom: exView.bottom
            anchors.left: exView.right
            width: parent.width
            clip: true
            visible: false
        }



        Label {
            id: mainLabel
            anchors.centerIn: parent
            font.pixelSize: 46
            font.bold: true
            text: currentExercise + "\n" + leftSeconds + " s"

        }

        EnginioModel {
            id: enginioModel
            client: EnginioClient {
                id: enginioClient
                backendId: "52dd09f3e5bde50ccc000cff"

                onError: console.log("Enginio error: " + reply.errorCode + ": " + reply.errorString)
                onFinished: console.log("Enginio finished: " + reply)
            }
            query: {"objectType": "objects.scheduleMap",
                    "query": {"schedule": scheduleName} }
        }

        Component.onCompleted: {
            console.log("onCompleted")
            timer.start()
        }

        Keys.onReleased: {
            if ( event.key === Qt.Key_Back && visible )
            {
                console.log("Back button captured - wunderbar !")
                event.accepted = true
                backPressed()
            }
        }
    }
}
